package com.yunhai.recipe.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.dto.Ret;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.model.entity.fms.FmsMaterialConfigEntity;
import com.yunhai.model.vo.fms.FmsMaterialConfigIntern;
import com.yunhai.model.vo.fms.FmsMaterialConfigQuery;
import com.yunhai.model.vo.fms.FmsMaterialConfigVo;
import com.yunhai.recipe.service.FmsMaterialConfigService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/materialConfig")
public class FmsMaterialConfigController {

    @Resource
    private FmsMaterialConfigService fmsMaterialConfigService;


    /**
     * 获取 FmsMaterialConfig 分页列表
     */
    @GetMapping("/pagination")
    public Ret getFmsMaterialConfigPagination(PaginationVo paginationVo, FmsMaterialConfigQuery fmsMaterialConfigQuery ) throws Exception {
        IPage page = fmsMaterialConfigService.getFmsMaterialConfigPagination(paginationVo, fmsMaterialConfigQuery );
        return Ret.toPage(page);
    }


    /**
     * 获取 FmsMaterialConfig 所有数据
     */
    @GetMapping("/all")
    public Ret getFmsMaterialConfigAllList( FmsMaterialConfigQuery fmsMaterialConfigQuery ) throws Exception {
        List<FmsMaterialConfigEntity> list = fmsMaterialConfigService.getFmsMaterialConfigAllList( fmsMaterialConfigQuery );
        return Ret.ok(list);
    }



    /**
     * 获取 FmsMaterialConfig 详情
    */
   @GetMapping("/{id}")
   public Ret getFmsMaterialConfigDetail(@PathVariable Long id) throws Exception {

       FmsMaterialConfigEntity fmsMaterialConfig = fmsMaterialConfigService.getFmsMaterialConfigDetail(id);
       return Ret.ok(fmsMaterialConfig);
   }

    @PostMapping("/import/excel")
    public Ret addImportExcelFile(@RequestParam("file") MultipartFile file ) throws Exception {

        fmsMaterialConfigService.addImportExcelFile( file );
        return Ret.ok();
    }


    @GetMapping("/export/excel")
    public void exportExcelFile(HttpServletResponse response) throws Exception {
        fmsMaterialConfigService.exportExcelFile(response);
    }



    @GetMapping("/condition")
    public List<FmsMaterialConfigIntern> getRecipeByIds(@RequestParam("ids") List<Long> ids ) throws Exception {

        List<FmsMaterialConfigEntity> details = fmsMaterialConfigService.getRecipeByIds(ids);
        if(ObjectUtil.isNotEmptyList(details)){
            List<FmsMaterialConfigIntern> results =  details.stream().map( item->{
                FmsMaterialConfigIntern row = new FmsMaterialConfigIntern();
                row.setId(item.getId());
                row.setName(item.getName());
                row.setNumber(item.getNumber());
                return row;
            }).collect(Collectors.toList());

            return results;
        }
        return new ArrayList<FmsMaterialConfigIntern>();
    }

    /**
     * 新增 FmsMaterialConfig
     * */
    @PostMapping("")
    public Ret addFmsMaterialConfig(@RequestBody FmsMaterialConfigVo fmsMaterialConfigVo) throws Exception {

        fmsMaterialConfigService.addFmsMaterialConfig(fmsMaterialConfigVo);
        return Ret.ok();
    }


    /**
     * 更新 FmsMaterialConfig
     * */
    @PutMapping("")
    public Ret updateFmsMaterialConfig(@RequestBody FmsMaterialConfigVo fmsMaterialConfigVo) throws Exception {

        fmsMaterialConfigService.updateFmsMaterialConfig(fmsMaterialConfigVo);
        return Ret.ok();
    }


    /**
     * 删除 FmsMaterialConfig
     * */
    @PostMapping("/batchRemove")
    public Ret batchRemoveFmsMaterialConfig( @RequestBody List<Long> ids ) throws Exception {

        fmsMaterialConfigService.batchRemoveFmsMaterialConfig(ids);
        return Ret.ok();
    }
}

  